<?php
class Database
{
    private $connection = null;
    private static $databaseObj;

    function __construct()
    {
        $this->connection = mysql_connect("localhost","root","1234") or die(mysql_error());
        mysql_select_db("ZooVision" ,$this->connection) or die(mysql_error());
    }
    public static function GetDataBaseObj()
    {
        if(Database::$databaseObj==null)
            Database::$databaseObj = new Database();
        return Database::$databaseObj;
    }

    public function InsertAndGetId($insert)
    {
        $result = mysql_query($insert,$this->connection);
        if($result==false)
            return 0;
        $insertId = mysql_insert_id($this->connection);
        return $insertId;
    }

    public function Insert($insert)
    {
        $result = mysql_query($insert,$this->connection);
        if($result==false)
            return false;
        return true;
    }

    public function GetOneRow($query)
    {
        $result = mysql_query($query,$this->connection);
        if($result==false || mysql_num_rows($result)<=0)
            return false;
        $row = mysql_fetch_assoc($result);
        return $row;
    }

    public function GetManyRows($query)
    {
        $rows = array();
        $result = mysql_query($query,$this->connection);
        if($result==false || mysql_num_rows($result)<=0)
            return $rows;
        while($aRow=mysql_fetch_array($result,MYSQL_ASSOC))
            $rows[] = $aRow;
        return $rows;
    }

    public function Update($update)
    {
        $rows = array();
        $result = mysql_query($update,$this->connection);
        if($result==false)
            return false;
        return true;
    }

    public function Delete($delete)
    {
        $rows = array();
        $result = mysql_query($delete,$this->connection);
        if($result==false)
            return false;
        return true;
    }
    public function GetTotalNumber($query){
        $result = mysql_query($query,$this->connection);
        $row=mysql_fetch_assoc($result);
        $totalNumber = $row['total_'];
        return $totalNumber;
    }
    public function NumRows($query){
        $result = mysql_query($query,$this->connection);
        return mysql_num_rows($result);
    }
    function __destruct() {
        mysql_close($this->connection);
    }
}
?>